<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Category\StoreRequest as CategoryStoreRequest;
use App\Jobs\Category\Query as CategoryQuery;
use App\Models\Category;
use App\Transformers\CategoryTransformer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $categories = $this->dispatchNow(new CategoryQuery($request->all(), true, self::PAGE_SIZE));

        return $this->response->paginator($categories, new CategoryTransformer(true));
    }


    /**
     * Store a newly created resource in storage.
     *
     * @author Vance
     *
     * @param CategoryStoreRequest $request
     * @return \Dingo\Api\Http\Response|void
     */
    public function store(CategoryStoreRequest $request)
    {
        $data = $request->only(['parent_id', 'title', 'description', 'sort']);

        if (!$category = Category::find($data['parent_id'])) {
            return $this->response->error('上级分类不存在', 422);
        }

        if (Category::Title($data['title'])->first()) {
            return $this->response->error('分类名称存在', 422);
        }

        $data['parent_id'] = end($data['parent_id']);

        Category::create($data);

        return $this->response->created();
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $category = Category::with('parent')->find($id);

        return $this->response->item($category, new CategoryTransformer());
    }

    /**
     * Update the specified resource in storage.
     *
     * @author Vance
     *
     * @param CategoryStoreRequest $request
     * @param $id
     * @return \Dingo\Api\Http\Response|void
     */
    public function update(CategoryStoreRequest $request, $id)
    {
        $data = $request->only(['parent_id', 'title', 'description', 'sort']);

        if (!$parent = Category::find($data['parent_id'])) {
            return $this->response->error('上级分类不存在', 422);
        }

        $category = Category::find($id);
        if (!$category) {
            return $this->response->error('分类信息不存在', 422);
        }
        $category->parent_id = end($data['parent_id']);
        $category->title = $data['title'];
        $category->description = $data['description'];
        $category->sort = $data['sort'];

        $category->save();

        return $this->response->created();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $category = Category::find($id);
        if (!$category) {
            return $this->response->error('分类信息不存在', 422);
        }

        if ($category->children()->count() > 0) {
            return $this->response->error('存在子级分类,请先删除子类', 422);
        }

        $category->delete();

        return $this->response->noContent();
    }
}
